<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>评论列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="./css/reset.css">
    <link rel="stylesheet" href="./css/iconfont.css">
    <link rel="stylesheet" href="./css/main.css">
    <!-- 分页控件样式 -->
    <link rel="stylesheet" href="libs/layui/css/layui.css">
    <link rel="stylesheet" href="libs/layui/css/modules/layer/default/layer.css">
    <script src="libs/layui/layui.js"></script>
    <script src="./libs/jquery/jquery.min.js"></script>
    <script src="libs/layui/lay/modules/layer.js"></script>
    <script src="js/login.js"></script>
    <!-- 引入模板引擎 -->
    <script src="libs/art-template/template-web.js"></script>
    <!-- 定义模板 -->
    <script type="text/html" id="act_comment">
        {{each data}} {{if $value.state =='已拒绝'}}
        <tr class="danger">
            {{else}}
            <tr>
                {{/if}}

                <td>{{$value.author}}</td>
                <td>{{$value.content}}</td>
                <td>{{$value.title}}</td>
                <td>{{$value.date}}</td>
                <td>{{$value.state}}</td>
                <td class="text-center">
                    {{if $value.state==='已通过'}}
                    <a href="javascript:void(0);;" class="btn btn-warning btn-xs" onclick="rejcet({{$value.id}})">拒绝</a> {{else if $value.state==='已拒绝'}}

                    <a href="javascript:void(0);;" class="btn btn-info btn-xs" onclick="pass({{$value.id}})">批准</a> {{else}}

                    <a href="javascript:void(0);;" class="btn btn-warning btn-xs" onclick="rejcet({{$value.id}})">拒绝</a>
                    <a href="javascript:void(0);;" class="btn btn-info btn-xs" onclick="pass({{$value.id}})">批准</a> {{/if}}

                    <a href="javascript:void(0);;" class="btn btn-danger btn-xs" onclick="delete_a({{$value.id}})">删除</a>
                </td>
            </tr>


            {{/each}}
    </script>
    <script>
        $(function() {
                //获取评论列表数据
                get_commentList()
            })
            //定义评论列表函数
        function get_commentList(curr, limit) {
            //获取到评论列表数据,就是定义ajax请求参数
            let pames = {
                page: curr,
                perpage: limit
            }
            myAjax('get', '/admin/comment/search', pames, function(resData) {
                // console.log(resData.data);
                //将响应回来的数据通过模板渲染到页面
                let resHtml = template('act_comment', resData.data)

                $('.table-hover>tbody').html(resHtml)
                    //调用分页控件方法
                laypage_control(resData.data.totalCount, curr)

            })

        }
        //分页控件函数
        function laypage_control(totalCount, curr_a) {
            layui.use('laypage', function() {
                var laypage = layui.laypage;

                //执行一个laypage实例
                laypage.render({
                    elem: 'control', //注意，这里的 test1 是 ID，不用加 # 号

                    count: totalCount, //数据总数，从服务端得到
                    curr: curr_a,
                    limit: 6,
                    jump: function(obj, first) {
                        //obj包含了当前分页的所有参数，比如：
                        // console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
                        // console.log(obj.limit); //得到每页显示的条数

                        // first 第一次将来是true，用户点击页码触发的时候 undefined
                        //首次不执行
                        if (!first) {
                            //do something
                            //调用评论列表方法
                            get_commentList(obj.curr, obj.limit)
                        }
                    }
                });
            })
        }

        //拒绝
        function rejcet(id_a) {
            layer.confirm('确定拒绝这条评论吗?', {
                icon: 3,
                title: '拒绝'
            }, function(index) {
                //do something
                //当用户点击时,发出拒绝ajax请求
                myAjax('post', '/admin/comment/reject', {
                    id: id_a
                }, function(resData) {
                    if (resData.code !== 200) {
                        layer.msg(resData.msg)
                        return
                    }
                    if (resData.code) layer.msg(resData.msg)

                    //重新刷新页面
                    get_commentList()

                })
                layer.close(index);
            })
        }

        //通过
        function pass(id_a) {
            layer.confirm('确定通过这条评论吗?', {
                icon: 3,
                title: '批准'
            }, function(index) {
                //do something
                //当用户点击时,发出通过ajax请求
                myAjax('post', '/admin/comment/pass', {
                    id: id_a
                }, function(resData) {
                    if (resData.code !== 200) {
                        layer.msg(resData.msg)
                        return
                    }
                    if (resData.code) layer.msg(resData.msg)

                    //重新刷新页面
                    get_commentList()
                })
                layer.close(index);
            })
        }

        //删除
        function delete_a(id_a) {
            layer.confirm('确定删除这条评论吗?', {
                icon: 3,
                title: '删除'
            }, function(index) {
                //do something
                //当用户点击时,发出删除ajax请求
                myAjax('post', '/admin/comment/delete', {
                    id: id_a
                }, function(resData) {
                    if (resData.code !== 200) {
                        layer.msg(resData.msg)
                        return
                    }
                    if (resData.code) layer.msg(resData.msg)

                    //重新刷新页面
                    get_commentList()
                })
                layer.close(index);
            })
        }
    </script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            评论列表
        </div>
        <div class="container-fluid common_con">
            <table class="table table-striped table-bordered table-hover mp20">
                <thead>
                    <tr>
                        <th>作者</th>
                        <th>评论</th>
                        <th>评论在</th>
                        <th>提交于</th>
                        <th>状态</th>
                        <th class="text-center" width="100">操作</th>
                    </tr>
                </thead>
                <tbody>


                </tbody>
            </table>
            <div id="control" class="row text-center">
                <!-- 动态生成分页控件 -->
            </div>
        </div>
    </div>
</body>

</html>